home *** CD-ROM | disk | FTP | other *** search
/ Programming a Multiplayer FPS in DirectX / Programming a Multiplayer FPS in DirectX (Companion CD).iso / DirectX / dxsdk_oct2004.exe / dxsdk.exe / Samples / Managed / Common / dxmutdata.cs < prev    next >
Encoding:
Text File  |  2004-09-27  |  21.7 KB  |  340 lines

  1. //--------------------------------------------------------------------------------------
  2. // File: DXMUTData.cs
  3. //
  4. // DirectX SDK Managed Direct3D sample framework data class
  5. //
  6. // Copyright (c) Microsoft Corporation. All rights reserved.
  7. //--------------------------------------------------------------------------------------
  8. using System;
  9. using System.Collections;
  10. using Microsoft.DirectX;
  11. using Microsoft.DirectX.Direct3D;
  12.  
  13. namespace Microsoft.Samples.DirectX.UtilityToolkit
  14. {
  15.  
  16.     #region Framework Interfaces and Eventargs classes
  17.     /// <summary>Interface that the framework will use to call into samples</summary>
  18.     public interface IFrameworkCallback
  19.     {
  20.         void OnFrameMove(Device device, double totalTime, float elapsedTime);
  21.         void OnFrameRender(Device device, double totalTime, float elapsedTime);
  22.     }
  23.     
  24.     /// <summary>Interface that the framework will use to determine if a device is acceptable</summary>
  25.     public interface IDeviceCreation
  26.     {
  27.         bool IsDeviceAcceptable(Caps caps, Format adapterFormat, Format backBufferFormat, bool isWindowed);
  28.         void ModifyDeviceSettings(DeviceSettings settings, Caps caps);
  29.     }
  30.  
  31.     /// <summary>Event arguments for device creation/reset</summary>
  32.     public class DeviceEventArgs : EventArgs
  33.     {
  34.         // Class data
  35.         public Device Device;
  36.         public SurfaceDescription BackBufferDescription;
  37.  
  38.         public DeviceEventArgs(Device d, SurfaceDescription desc) 
  39.         {
  40.             Device = d;
  41.             BackBufferDescription = desc;
  42.         }
  43.     }
  44.     /// <summary>Event Handler delegate for device creation/reset</summary>
  45.     public delegate void DeviceEventHandler(object sender, DeviceEventArgs e);
  46.     #endregion
  47.  
  48.     #region Device Settings
  49.     /// <summary>
  50.     /// Holds the settings for creating a device
  51.     /// </summary>
  52.     public class DeviceSettings : ICloneable
  53.     {
  54.         public uint AdapterOrdinal;
  55.         public DeviceType DeviceType;
  56.         public Format AdapterFormat;
  57.         public CreateFlags BehaviorFlags;
  58.         public PresentParameters presentParams;
  59.  
  60.         #region ICloneable Members
  61.         /// <summary>Clone this object</summary>
  62.         public DeviceSettings Clone()
  63.         {
  64.             DeviceSettings clonedObject = new DeviceSettings();
  65.             clonedObject.presentParams = (PresentParameters)this.presentParams.Clone();
  66.             clonedObject.AdapterFormat = this.AdapterFormat;
  67.             clonedObject.AdapterOrdinal = this.AdapterOrdinal;
  68.             clonedObject.BehaviorFlags = this.BehaviorFlags;
  69.             clonedObject.DeviceType = this.DeviceType;
  70.  
  71.             return clonedObject;
  72.         }
  73.         /// <summary>Clone this object</summary>
  74.         object ICloneable.Clone() { throw new NotSupportedException("Use the strongly typed overload instead."); }
  75.         #endregion
  76.     }
  77.     #endregion
  78.  
  79.     #region User Timers
  80.     /// <summary>Stores timer callback information</summary>
  81.     public struct TimerData
  82.     {
  83.         public TimerCallback callback;
  84.         public float TimeoutInSecs;
  85.         public float Countdown;
  86.         public bool IsEnabled;
  87.     }
  88.     #endregion
  89.  
  90.     #region Callback methods 
  91.     public delegate void KeyboardCallback(System.Windows.Forms.Keys key, bool keyDown, bool altDown);
  92.     public delegate void MouseCallback(bool leftButtonDown, bool rightButtonDown, bool middleButtonDown, bool sideButton1Down, bool sideButton2Down, int mouseWheelDelta, int x, int y);
  93.     public delegate IntPtr WndProcCallback(IntPtr hWnd, NativeMethods.WindowMessage msg, IntPtr wParam, IntPtr lParam, ref bool NoFurtherProcessing);
  94.     public delegate void TimerCallback(uint eventId);
  95.     #endregion
  96.  
  97.     #region Matching Options
  98.     /// <summary>
  99.     /// Used when finding valid device settings
  100.     /// </summary>
  101.     public enum MatchType
  102.     {
  103.         IgnoreInput, // Use the closest valid value to a default 
  104.         PreserveInput, // Use input without change, but may cause no valid device to be found
  105.         ClosestToInput // Use the closest valid value to the input 
  106.     }
  107.  
  108.     /// <summary>
  109.     /// Options on how to match items
  110.     /// </summary>
  111.     public struct MatchOptions
  112.     {
  113.         public MatchType AdapterOrdinal;
  114.         public MatchType DeviceType;
  115.         public MatchType Windowed;
  116.         public MatchType AdapterFormat;
  117.         public MatchType VertexProcessing;
  118.         public MatchType Resolution;
  119.         public MatchType BackBufferFormat;
  120.         public MatchType BackBufferCount;
  121.         public MatchType MultiSample;
  122.         public MatchType SwapEffect;
  123.         public MatchType DepthFormat;
  124.         public MatchType StencilFormat;
  125.         public MatchType PresentFlags;
  126.         public MatchType RefreshRate;
  127.         public MatchType PresentInterval;
  128.     };
  129.     #endregion
  130.  
  131.     #region Framework's data
  132.     /// <summary>
  133.     /// Holds data for the Framework class, and all of the properties
  134.     /// </summary>
  135.     class FrameworkData
  136.     {
  137.         #region Instance Data
  138.         private Device device; // the D3D rendering device
  139.  
  140.         private DeviceSettings  currentDeviceSettings; // current device settings
  141.         private SurfaceDescription backBufferSurfaceDesc; // back buffer surface description
  142.         private Caps caps; // D3D caps for current device
  143.  
  144.         private IntPtr windowFocus; // the main app focus window
  145.         private IntPtr windowDeviceFullScreen; // the main app device window in fullscreen mode
  146.         private IntPtr windowDeviceWindowed; // the main app device window in windowed mode
  147.         private IntPtr adapterMonitor; // the monitor of the adapter 
  148.         private double currentTime; // current time in seconds
  149.         private float elapsedTime; // time elapsed since last frame
  150.  
  151.         private System.Windows.Forms.FormStartPosition defaultStartingLocation; // default starting location of the window
  152.         private System.Drawing.Rectangle clientRect; // client rect of window
  153.         private System.Drawing.Rectangle fullScreenClientRect; // client rect of window when fullscreen
  154.         private System.Drawing.Rectangle windowBoundsRect; // window rect of window
  155.         private System.Drawing.Point windowLocation; // Location of the window
  156.         private NativeMethods.WindowStyles windowStyle; // Style of the window
  157.         
  158.         private System.Windows.Forms.MainMenu windowMenu; // menu of app
  159.         private double lastStatsUpdateTime; // last time the stats were updated
  160.         private uint lastStatsUpdateFrames; // frames count since last time the stats were updated
  161.         private float frameRate; // frames per second
  162.         private int currentFrameNumber; // the current frame number
  163.  
  164.         private bool isHandlingDefaultHotkeys; // if true, the sample framework will handle some default hotkeys
  165.         private bool isShowingMsgBoxOnError; // if true, then msgboxes are displayed upon errors
  166.         private bool isClipCursorWhenFullScreen; // if true, then the sample framework will keep the cursor from going outside the window when full screen
  167.         private bool isShowingCursorWhenFullScreen; // if true, then the sample framework will show a cursor when full screen
  168.         private bool isConstantFrameTime; // if true, then elapsed frame time will always be 0.05f seconds which is good for debugging or automated capture
  169.         private float timePerFrame; // the constant time per frame in seconds, only valid if isConstantFrameTime==true
  170.         private bool isInWireframeMode; // if true, then RenderState.FillMode==FillMode.WireFrame else RenderState.FillMode==FillMode.Solid
  171.         private bool canAutoChangeAdapter; // if true, then the adapter will automatically change if the window is different monitor
  172.         private bool isWindowCreatedWithDefaultPositions; // if true, then default was used and the window should be moved to the right adapter
  173.         private int applicationExitCode; // the exit code to be returned to the command line
  174.  
  175.         private bool isInited; // if true, then Init() has succeeded
  176.         private bool wasWindowCreated; // if true, then CreateWindow() or SetWindow() has succeeded
  177.         private bool wasDeviceCreated; // if true, then CreateDevice*() or SetDevice() has succeeded
  178.  
  179.         private bool isInitCalled; // if true, then Init() was called
  180.         private bool isWindowCreateCalled; // if true, then CreateWindow() or SetWindow() was called
  181.         private bool isDeviceCreateCalled; // if true, then CreateDevice*() or SetDevice() was called
  182.  
  183.         private bool isDeviceObjectsCreated; // if true, then DeviceCreated callback has been called (if non-NULL)
  184.         private bool isDeviceObjectsReset; // if true, then DeviceReset callback has been called (if non-NULL)
  185.         private bool isInsideDeviceCallback; // if true, then the framework is inside an app device callback
  186.         private bool isInsideMainloop; // if true, then the framework is inside the main loop
  187.         private bool isActive; // if true, then the app is the active top level window
  188.         private bool isTimePaused; // if true, then time is paused
  189.         private bool isRenderingPaused; // if true, then rendering is paused
  190.         private int pauseRenderingCount; // pause rendering ref count
  191.         private int pauseTimeCount; // pause time ref count
  192.         private bool isDeviceLost; // if true, then the device is lost and needs to be reset
  193.         private bool isMinimized; // if true, then the window is minimized
  194.         private bool isMaximized; // if true, then the window is maximized
  195.         private bool isSizeChangesIgnored; // if true, the sample framework won't reset the device upon window size change (for public use only)
  196.         private bool isNotifyOnMouseMove; // if true, include WM_MOUSEMOVE in mousecallback
  197.  
  198.         private int overrideAdapterOrdinal; // if != -1, then override to use this adapter ordinal
  199.         private bool overrideWindowed; // if true, then force to start windowed
  200.         private bool overrideFullScreen; // if true, then force to start full screen
  201.         private int overrideStartX; // if != -1, then override to this X position of the window
  202.         private int overrideStartY; // if != -1, then override to this Y position of the window
  203.         private int overrideWidth; // if != 0, then override to this width
  204.         private int overrideHeight; // if != 0, then override to this height
  205.         private bool overrideForceHAL; // if true, then force to Hardware device (failing if one doesn't exist)
  206.         private bool overrideForceREF; // if true, then force to Reference device (failing if one doesn't exist)
  207.         private bool overrideForcePureHWVP; // if true, then force to use pure Hardware VertexProcessing (failing if device doesn't support it)
  208.         private bool overrideForceHWVP; // if true, then force to use Hardware VertexProcessing (failing if device doesn't support it)
  209.         private bool overrideForceSWVP; // if true, then force to use Software VertexProcessing 
  210.         private bool overrideConstantFrameTime; // if true, then force to constant frame time
  211.         private float overrideConstantTimePerFrame; // the constant time per frame in seconds if overrideConstantFrameTime==true
  212.         private int overrideQuitAfterFrame; // if != 0, then it will force the app to quit after that frame
  213.  
  214.         private IDeviceCreation deviceCallback; // Callback for device creation and acceptability
  215.         private IFrameworkCallback frameworkCallback; // Framework callback interface
  216.         private KeyboardCallback keyboardFunc; // keyboard callback
  217.         private MouseCallback mouseFunc; // mouse callback
  218.         private WndProcCallback wndFunc; // window messages callback
  219.  
  220.         private SettingsDialog settings; // The settings dialog
  221.         private bool isShowingD3DSettingsDlg; // if true, then show the settings dialog
  222.  
  223.         private ArrayList timerList = new ArrayList(); // list of TimerData structs
  224.         private string staticFrameStats; // static part of frames stats 
  225.         private string frameStats; // frame stats (fps, width, etc)
  226.         private string deviceStats; // device stats (description, device type, etc)
  227.         private string windowTitle; // window title
  228.  
  229.         private NativeMethods.WndProcDelegate windowDelegate; // Delegate for windows proc
  230.         #endregion
  231.  
  232.         #region Properties
  233.         public Device Device { get { return device; } set {device = value; } }
  234.         public DeviceSettings CurrentDeviceSettings { get { return currentDeviceSettings; } set {currentDeviceSettings = value; } }
  235.         public SurfaceDescription BackBufferSurfaceDesc { get { return backBufferSurfaceDesc; } set {backBufferSurfaceDesc = value; } }
  236.         public Caps Caps { get { return caps; } set {caps = value; } }
  237.  
  238.         public IntPtr WindowFocus { get { return windowFocus; } set {windowFocus = value; } }
  239.         public IntPtr WindowDeviceFullScreen { get { return windowDeviceFullScreen; } set {windowDeviceFullScreen = value; } }
  240.         public IntPtr WindowDeviceWindowed { get { return windowDeviceWindowed; } set {windowDeviceWindowed = value; } }
  241.         public IntPtr AdapterMonitor { get { return adapterMonitor; } set {adapterMonitor = value; } }
  242.         public double CurrentTime { get { return currentTime; } set {currentTime = value; } }
  243.         public float ElapsedTime { get { return elapsedTime; } set {elapsedTime = value; } }
  244.         public NativeMethods.WindowStyles WindowStyle { get { return windowStyle; } set {windowStyle = value; } }
  245.  
  246.         public System.Windows.Forms.FormStartPosition DefaultStartingLocation { get { return defaultStartingLocation; } set {defaultStartingLocation = value; } }
  247.         public System.Drawing.Rectangle ClientRectangle { get { return clientRect; } set {clientRect = value; } }
  248.         public System.Drawing.Rectangle FullScreenClientRectangle { get { return fullScreenClientRect; } set {fullScreenClientRect = value; } }
  249.         public System.Drawing.Rectangle WindowBoundsRectangle { get { return windowBoundsRect; } set {windowBoundsRect = value; } }
  250.         public System.Drawing.Point ClientLocation { get { return windowLocation; } set {windowLocation = value; } }
  251.         public System.Windows.Forms.MainMenu Menu { get { return windowMenu; } set {windowMenu = value; } }
  252.         public double LastStatsUpdateTime { get { return lastStatsUpdateTime; } set {lastStatsUpdateTime = value; } }
  253.         public uint LastStatsUpdateFrames { get { return lastStatsUpdateFrames; } set {lastStatsUpdateFrames = value; } }
  254.         public float CurrentFrameRate { get { return frameRate; } set {frameRate = value; } }
  255.         public int CurrentFrameNumber { get { return currentFrameNumber; } set {currentFrameNumber = value; } }
  256.  
  257.         public bool IsHandlingDefaultHotkeys { get { return isHandlingDefaultHotkeys; } set {isHandlingDefaultHotkeys = value; } }
  258.         public bool IsShowingMsgBoxOnError { get { return isShowingMsgBoxOnError; } set {isShowingMsgBoxOnError = value; } }
  259.         public bool IsCursorClippedWhenFullScreen { get { return isClipCursorWhenFullScreen; } set {isClipCursorWhenFullScreen = value; } }
  260.         public bool IsShowingCursorWhenFullScreen { get { return isShowingCursorWhenFullScreen; } set {isShowingCursorWhenFullScreen = value; } }
  261.         public bool IsUsingConstantFrameTime { get { return isConstantFrameTime; } set {isConstantFrameTime = value; } }
  262.         public float TimePerFrame { get { return timePerFrame; } set {timePerFrame = value; } }
  263.         public bool IsInWireframeMode { get { return isInWireframeMode; } set {isInWireframeMode = value; } }
  264.         public bool CanAutoChangeAdapter { get { return canAutoChangeAdapter; } set {canAutoChangeAdapter = value; } }
  265.         public bool IsWindowCreatedWithDefaultPositions { get { return isWindowCreatedWithDefaultPositions; } set {isWindowCreatedWithDefaultPositions = value; } }
  266.         public int ApplicationExitCode { get { return applicationExitCode; } set {applicationExitCode = value; } }
  267.  
  268.         public bool IsInited { get { return isInited; } set {isInited = value; } }
  269.         public bool WasWindowCreated { get { return wasWindowCreated; } set {wasWindowCreated = value; } }
  270.         public bool WasDeviceCreated { get { return wasDeviceCreated; } set {wasDeviceCreated = value; } }
  271.  
  272.         public bool WasInitCalled { get { return isInitCalled; } set {isInitCalled = value; } }
  273.         public bool WasWindowCreateCalled { get { return isWindowCreateCalled; } set {isWindowCreateCalled = value; } }
  274.         public bool WasDeviceCreateCalled { get { return isDeviceCreateCalled; } set {isDeviceCreateCalled = value; } }
  275.  
  276.         public bool AreDeviceObjectsCreated { get { return isDeviceObjectsCreated; } set {isDeviceObjectsCreated = value; } }
  277.         public bool AreDeviceObjectsReset { get { return isDeviceObjectsReset; } set {isDeviceObjectsReset = value; } }
  278.         public bool IsInsideDeviceCallback { get { return isInsideDeviceCallback; } set {isInsideDeviceCallback = value; } }
  279.         public bool IsInsideMainloop { get { return isInsideMainloop; } set {isInsideMainloop = value; } }
  280.         public bool IsActive { get { return isActive; } set {isActive = value; } }
  281.         public bool IsTimePaused { get { return isTimePaused; } set {isTimePaused = value; } }
  282.         public bool IsRenderingPaused { get { return isRenderingPaused; } set {isRenderingPaused = value; } }
  283.         public int PauseRenderingCount { get { return pauseRenderingCount; } set {pauseRenderingCount = value; } }
  284.         public int PauseTimeCount { get { return pauseTimeCount; } set {pauseTimeCount = value; } }
  285.         public bool IsDeviceLost { get { return isDeviceLost; } set {isDeviceLost = value; } }
  286.         public bool IsMinimized { get { return isMinimized; } set {isMinimized = value; } }
  287.         public bool IsMaximized { get { return isMaximized; } set {isMaximized = value; } }
  288.         public bool AreSizeChangesIgnored { get { return isSizeChangesIgnored; } set {isSizeChangesIgnored = value; } }
  289.         public bool IsNotifiedOnMouseMove { get { return isNotifyOnMouseMove; } set {isNotifyOnMouseMove = value; } }
  290.  
  291.         public int OverrideAdapterOrdinal { get { return overrideAdapterOrdinal; } set {overrideAdapterOrdinal = value; } }
  292.         public bool IsOverridingWindowed { get { return overrideWindowed; } set {overrideWindowed = value; } }
  293.         public bool IsOverridingFullScreen { get { return overrideFullScreen; } set {overrideFullScreen = value; } }
  294.         public int OverrideStartX { get { return overrideStartX; } set {overrideStartX = value; } }
  295.         public int OverrideStartY { get { return overrideStartY; } set {overrideStartY = value; } }
  296.         public int OverrideWidth { get { return overrideWidth; } set {overrideWidth = value; } }
  297.         public int OverrideHeight { get { return overrideHeight; } set {overrideHeight = value; } }
  298.         public bool IsOverridingForceHardware { get { return overrideForceHAL; } set {overrideForceHAL = value; } }
  299.         public bool IsOverridingForceReference { get { return overrideForceREF; } set {overrideForceREF = value; } }
  300.         public bool IsOverridingForcePureHardwareVertexProcessing { get { return overrideForcePureHWVP; } set {overrideForcePureHWVP = value; } }
  301.         public bool IsOverridingForceHardwareVertexProcessing { get { return overrideForceHWVP; } set {overrideForceHWVP = value; } }
  302.         public bool IsOverridingForceSoftwareVertexProcessing { get { return overrideForceSWVP; } set {overrideForceSWVP = value; } }
  303.         public bool IsOverridingConstantFrameTime { get { return overrideConstantFrameTime; } set {overrideConstantFrameTime = value; } }
  304.         public float OverrideConstantTimePerFrame { get { return overrideConstantTimePerFrame; } set {overrideConstantTimePerFrame = value; } }
  305.         public int OverrideQuitAfterFrame { get { return overrideQuitAfterFrame; } set {overrideQuitAfterFrame = value; } }
  306.  
  307.         public IDeviceCreation DeviceCreationInterface { get { return deviceCallback; } set { deviceCallback = value; } }
  308.         public IFrameworkCallback CallbackInterface { get { return frameworkCallback; } set {frameworkCallback = value; } }
  309.         public KeyboardCallback KeyboardFunction { get { return keyboardFunc; } set {keyboardFunc = value; } }
  310.         public MouseCallback MouseFunction { get { return mouseFunc; } set {mouseFunc = value; } }
  311.         public WndProcCallback WndProcFunction { get { return wndFunc; } set {wndFunc = value; } }
  312.         
  313.         public SettingsDialog Settings { get { return settings; } set {settings = value; } }
  314.         public bool IsD3DSettingsDialogShowing { get { return isShowingD3DSettingsDlg; } set {isShowingD3DSettingsDlg = value; } }
  315.  
  316.         public ArrayList Timers { get { return timerList; } set {timerList = value; } }
  317.         public string StaticFrameStats { get { return staticFrameStats; } set {staticFrameStats = value; } }
  318.         public string FrameStats { get { return frameStats; } set {frameStats = value; } }
  319.         public string DeviceStats { get { return deviceStats; } set {deviceStats = value; } }
  320.         public string WindowTitle { get { return windowTitle; } set {windowTitle = value; } }
  321.         public NativeMethods.WndProcDelegate WindowProcDelegate { get { return windowDelegate; } set {windowDelegate = value; } }
  322.         #endregion
  323.  
  324.         /// <summary>
  325.         /// Initialize data
  326.         /// </summary>
  327.         public FrameworkData()
  328.         {
  329.             // Set some initial data
  330.             overrideStartX = -1;
  331.             overrideStartY = -1;
  332.             overrideAdapterOrdinal = -1;
  333.             canAutoChangeAdapter = true;
  334.             isShowingMsgBoxOnError = true;
  335.             isActive = true;
  336.             defaultStartingLocation = System.Windows.Forms.FormStartPosition.WindowsDefaultLocation;
  337.         }
  338.     }
  339.     #endregion
  340. }